package com.jiajie.gsaccessingdatajpa.concurrent;

import java.util.LinkedList;
import java.util.Queue;

/**
 * @author jie
 * @date 2021/8/21 8:23 AM
 */
public class TaskQueue {
    Queue<String> queue = new LinkedList<>();

    public synchronized void addTask(String s){
        this.queue.add(s);
        // 唤醒在this锁等待的线程
        this.notifyAll();
    }

    public synchronized String getTask() throws InterruptedException {
        while (queue.isEmpty()){
            // 释放this锁
            this.wait();
            // 重新获取this锁
        }
        return queue.remove();
    }
}
